
layui.use(['form', 'layer', 'table', 'formSelects'], function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;
    var formSelects = layui.formSelects;

    var datalist_url = $('.datalist_url').val();
    var add_url = $('.add_url').val();
    var edit_url = $('.edit_url').val();
    var detail_url = $('.detail_url').val();
    var delete_url = $('.delete_url').val();
    var save_url = $('.save_url').val();

    var select_products_url = $('.select_products_url').val();


    if($('#storeIds').length > 0){
        formSelects.render('storeIds');
    }

    // Table 列表
    var tableIns = table.render({
        elem: '#dataTable',
        url : datalist_url,
        page : true,
        cellMinWidth : 95,
        //height : "full-100",
        limit : 10,
        limits : [10, 20, 50, 100],
        id : "tableList",
        cols : [[
            /*{field: 'id', title: '序号', align:'center', width:60},*/
            {field: 'order_no', title: '调价单号', align:'center', minWidth:110},
            {field: 'count_product', title: '调价商品数', align:'center'},
            {field: 'range', title: '调价范围', align:'center', minWidth:240},
            {field: 'create_date', title: '生效日期', align:'left', minWidth:110},
            {field: 'status', title: '状态', align:'center'},
            {field: 'create_oper', title: '提交人', align:'center'},
            {title: '操作', fixed:"right", align:"center", minWidth:200, templet:function(data){
                var html = '';
                html += '<a href="'+detail_url+'?id='+data.id+'" class="layui-btn layui-btn-xs">详情</a>';
                html += '<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>';
                html += '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>';

                return html;
            }}
        ]]
    });

    // 搜索
    $(".search_btn").on("click", function(){
        if($('.name').val() != ''){
            table.reload("tableList", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    order_no: $('.searchBox').find('input[name=order_no]').val()  // 搜索的关键字
                }
            });
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    // 清空搜索
    $(".reset_btn").on("click", function(){
        table.reload("tableList", {
            where: {
                order_no: ''
            }
        });
    });

    $('.add_btn').on('click', function(){
        formHtml();
    });

    // 列表操作
    table.on('tool(dataTable)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        // 删除
        if (layEvent === 'del') {
            layer.confirm('确定删除此数据？',{icon:3, title:'提示信息'},function(index){
                $.ajax({
                    url: delete_url,
                    data: {"id": data.id},
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        if (res.code == 1) {
                            layer.msg('已删除', {icon: 1, time: 1000});
                            tableIns.reload();
                            layer.close(index);
                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (data) {
                        layer.msg("服务器无响应");
                    }
                });
            });
        } else if(layEvent === 'edit'){
            formHtml(data);
        }
    });

    // 删除
    /*$('.del-btn').on('click', function(){
        layer.confirm('确定删除此数据？',{icon:3, title:'提示信息'},function(index){
            $.ajax({
                url: delete_url,
                data: {"id": data.id},
                type: "POST",
                dataType: "json",
                success: function (res) {
                    if (res.code == 1) {
                        layer.msg('已删除', {icon: 1, time: 1000});
                        tableIns.reload();
                        layer.close(index);
                    } else {
                        layer.msg(res.msg);
                    }
                },
                error: function (data) {
                    layer.msg("服务器无响应");
                }
            });
        });
    });*/

    // Form 表单
    function formHtml(data){
        var content = add_url;
        if(data){
            content = edit_url + '?id=' + data.id;
        }
        // 右侧面板
        var index = layer.open({
            type: 2
            , anim: -1
            , title: false
            , closeBtn: false
            , offset: 'r'
            , shade: 0.1
            //, shadeClose: true // 是否点击遮罩关闭
            , skin: 'layui-anim layui-anim-rl layui-layer-adminRight'
            , area: '800px'
            , top: 0
            , content: content
        });
    }

    if($('.range').attr('data-value')){
        initRange($('.range').attr('data-value'));
    }
    function initRange(value){
        var value = $('.range').val();
        if (value == 5) {
            $('.store_ids-div').css('display', 'block');
        } else {
            $('.store_ids-div').css('display', 'none');
        }
    }
    // 切换调价范围
    form.on('select(range)', function (data) {
        var value = data.value;
        initRange(value);
    });

    // 自定义验证规则
    form.verify({
        product_ids: function(value){
            if (value == '') {
                return '请选择商品';
            }
        },
        store_ids: function(value){
            if($('.range').val() == 5){
                if (value == '') {
                    return '请选择指定门店';
                }
            }
        },
    });

    // 表单提交
    form.on("submit(laySave)", function(data){
        // 弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        $.ajax({
            url: save_url,
            data: data.field,
            type: "post",
            dataType: "json",
            success: function(res){
                top.layer.close(index);
                top.layer.msg(res.msg);
                if(res.code == 1){
                    layer.closeAll("iframe");
                    parent.layui.table.reload('tableList', {});
                }
            },
            error:function(data){
                layer.msg("服务器无响应");
            }
        });
    });

    // 选择商品
    $('.select-products').on('click', function(){
        var content = select_products_url;
        var index = layer.open({
            title : "选择商品",
            type : 2,
            area : ["800px", "600px"],
            content : content,
            success : function(layero, index){
                form.render();
            }
        });
    });

});